import csv
import requests

url = "https://iftp.chinamoney.com.cn/ags/ms/cm-u-bond-md/BondMarketInfoListEN"

# 创建CSV文件并写入列名
csv_file = 'bond_data.csv'
fields = ['ISIN', 'BondCode', 'Issuer', 'BondType', 'IssueDate', 'LatestRating']
with open(csv_file, 'w', newline='', encoding='utf-8') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=fields)
    writer.writeheader()

    # 请求数据并写入CSV文件
    for page in range(1, 9):
        data = {
            "pageNo": page,
            "pageSize": 15,
            "isin": "",
            "bondCode": "",
            "issueEnty": "",
            "bondType": 100001,
            "couponType": "",
            "issueYear": 2023,
            "rtngShrt": "",
            "bondSpclPrjctVrty": ""
        }
        response = requests.post(url, data)
        response_data = response.json()['data']['resultList']
        for data in response_data:
            ISIN = data['isin']
            BondCode = data['bondCode']
            Issuer = data['entyFullName']
            BondType = data['bondType']
            IssueDate = data['issueStartDate']
            LatestRating = data['debtRtng']
            writer.writerow({'ISIN': ISIN, 'BondCode': BondCode, 'Issuer': Issuer, 'BondType': BondType, 'IssueDate': IssueDate, 'LatestRating': LatestRating})

print("CSV文件保存成功！")